package com.daidai.base

import android.app.Dialog
import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import com.daidai.spinner.R

abstract class BaseDialog(open var ctx: Context, styleId:Int = R.style.MyDialog) : Dialog(ctx,styleId) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window?.let {
            val rect =  Rect();
            val view1 = it.decorView;
            view1.getWindowVisibleDisplayFrame(rect)
            it.setBackgroundDrawableResource(android.R.color.transparent)
            val params = it.attributes
            params.width = rect.width()
            params.height = rect.height()
            it.attributes = params
        }

        setContentView(getContentViewResId())

        // 取消点击空白关闭
        setCanceledOnTouchOutside(false)
        setCancelable(false)

        initView()
        initData()
    }

    abstract fun getContentViewResId():Int
    abstract fun initView()
    abstract fun initData()

    fun findBtn(id:Int) = findViewById<View>(id)

//    fun context2Activity():Activity{
//        var context = this.ctx
//        while (context is ContextWrapper){
//            if (context is Activity){
//                return context as Activity
//            }else{
//                context = context.baseContext
//            }
//        }
//        throw RuntimeException("context can not to be")
//    }
}